import { getNewValue } from ".";
import { uniqueId } from 'lodash';

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const { sheetName, row, table, store, old } = options;

        //dataTable改造
        const tableName = table.name().replace("detail", "detail_");
        const updateJson = window.updateJson[sheetName] ? window.updateJson[sheetName][tableName] : [];
        updateJson.map(rowObj => {
            if (rowObj.row === row + 1) {
                window.updateJson[sheetName][tableName].map(r => {
                    if (r.row >= rowObj.row) r.row += 1;
                })
            }
        })
        if (updateJson.length > 0) {
            if (window.updateJson[sheetName][tableName]) {
                window.updateJson[sheetName][tableName].push({ row: row + 1, id: uniqueId("-") });
            } else {
                window.updateJson[sheetName][tableName] = [{ row: row + 1, id: uniqueId("-") }];
            }
        }
        console.log("updateJson", window.updateJson)



        context.suspendPaint();
        try {
            const bindingPath = table.bindingPath();
            if (isUndo) {
                store.dataSourceInstance.setValue(bindingPath, old.tableValue);
            } else {
                const tableValue = store.dataSourceInstance.getValue(bindingPath);
                options.old = { tableValue: [...tableValue] };

                const { row: startRow } = table.dataRange();
                const rowIndex = row - startRow;
                const newValue = getNewValue(tableValue, rowIndex, []);
                store.dataSourceInstance.setValue(bindingPath, newValue);
            }
            table.bindingPath(bindingPath);
            store.calcAll(true, sheetName);
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
        context.resumePaint();
    }
};